package com.finace.money.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.TickBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhangpeng on 2017/12/1.
 */

public class DealListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<TickBean> mTickList;
    private TickBean mTickBean;

    public void setmTickBean(TickBean mTickBean) {
        this.mTickBean = mTickBean;
    }

    public void setmTickList(List<TickBean> mTickList) {
        this.mTickList = mTickList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DealListHolder(LayoutInflater.from(UIUtil.getContext()).inflate(R.layout.item_deal_detail, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof DealListHolder) {
            DealListHolder dealListHolder = (DealListHolder) holder;
            dealListHolder.text1.setTextSize(16);
            dealListHolder.text2.setTextSize(16);
            dealListHolder.text3.setTextSize(16);
            dealListHolder.text4.setTextSize(16);
            dealListHolder.text5.setTextSize(16);
            dealListHolder.text4.setTextColor(UIUtil.getColor(R.color.colorWhite));

            if (null != mTickList && mTickList.size() > 0) {
                TickBean tickBean = mTickList.get(position);
                TickBean preTickBean;
                if (null != tickBean) {
                    if (position == 0) {
                        if (null != mTickBean) {
                            preTickBean = mTickBean;
                        } else {
                            preTickBean = null;
                        }
                        dealListHolder.text5.setText("-");
                    } else {
                        preTickBean = mTickList.get(position - 1);
                    }
                    if (null != preTickBean) {
                        double addOi = Double.valueOf(tickBean.getOi()) - Double.valueOf(preTickBean.getOi());
                        double addTrade = Double.valueOf(tickBean.getTrade()) - Double.valueOf(preTickBean.getTrade());
                        double addVol = Double.valueOf(tickBean.getVol()) - Double.valueOf(preTickBean.getVol());

                        if (addOi == Double.valueOf(tickBean.getSize()) && addTrade > 0) {
                            dealListHolder.text5.setText(R.string.duo_kai);//红
                            dealListHolder.text3.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                            dealListHolder.text5.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                        } else if (addOi == Double.valueOf(tickBean.getSize()) && addTrade < 0) {
                            dealListHolder.text5.setText(R.string.kong_kai);//绿
                            dealListHolder.text3.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                            dealListHolder.text5.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                        } else if (addOi == -Double.valueOf(tickBean.getSize()) && addTrade > 0) {
                            dealListHolder.text5.setText(R.string.duo_ping);//绿
                            dealListHolder.text3.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                            dealListHolder.text5.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                        } else if (addOi == -Double.valueOf(tickBean.getSize()) && addTrade < 0) {
                            dealListHolder.text5.setText(R.string.kong_ping);//红
                            dealListHolder.text3.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                            dealListHolder.text5.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                        } else if (addOi == 2 * Double.valueOf(tickBean.getSize()) && addVol == addOi) {
                            dealListHolder.text5.setText(R.string.double_kai);//白
                            dealListHolder.text3.setTextColor(UIUtil.getColor(R.color.colorWhite));
                            dealListHolder.text5.setTextColor(UIUtil.getColor(R.color.colorWhite));
                        } else if (addOi == -2 * Double.valueOf(tickBean.getSize())) {
                            dealListHolder.text5.setText(R.string.double_ping);//白
                            dealListHolder.text3.setTextColor(UIUtil.getColor(R.color.colorWhite));
                            dealListHolder.text5.setTextColor(UIUtil.getColor(R.color.colorWhite));
                        } else {
                            dealListHolder.text5.setText("-");//白
                            dealListHolder.text3.setTextColor(UIUtil.getColor(R.color.colorWhite));
                            dealListHolder.text5.setTextColor(UIUtil.getColor(R.color.colorWhite));
                        }
                        if (Double.valueOf(tickBean.getTrade()) > Double.valueOf(tickBean.getPresettle())) {
                            dealListHolder.text2.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                        } else {
                            dealListHolder.text2.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                        }

                        dealListHolder.text1.setText(tickBean.getTime());
                        dealListHolder.text2.setText(tickBean.getTrade());
                        dealListHolder.text3.setText(tickBean.getSize());
                        dealListHolder.text4.setText(tickBean.getSize());
                    }

                } else {
                    dealListHolder.text1.setText("-");
                    dealListHolder.text2.setText("-");
                    dealListHolder.text3.setText("-");
                    dealListHolder.text4.setText("-");
                    dealListHolder.text5.setText("-");
                }

            }
        }
    }

    @Override
    public int getItemCount() {
        return null != mTickList && mTickList.size() > 0 ? mTickList.size() : 0;
    }

    public class DealListHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.text_1)
        TextView text1;
        @BindView(R.id.text_2)
        TextView text2;
        @BindView(R.id.text_3)
        TextView text3;
        @BindView(R.id.text_4)
        TextView text4;
        @BindView(R.id.text_5)
        TextView text5;

        public DealListHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
